<template>
  <div class="login-container">
    <div class="login-box">
      <p class="title">欢迎使用校园一卡通服务平台后台管理系统</p>
      <el-form ref="form" :rules="rules" :model="userInfo" label-width="80px" hide-required-asterisk>
        <el-form-item label="用户名" prop="studentNum">
          <el-input v-model="userInfo.studentNum"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="userInfo.password" show-password></el-input>
        </el-form-item>
      </el-form>
      <div class="btn-box">
        <el-button type="primary" @click="login('form')">登录</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import commonServe from '../api/services/common.serve'
export default {
  data() {
    return {
      userInfo: {
        studentNum: 'admin',
        password: '1234567'
      },

      rules: {
        studentNum: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    login(val) {
      this.$refs[val].validate((valid) => {
        if (!valid) return
        // 临时使用
        // window.sessionStorage.setItem('id', 1)
        commonServe
          .login(this.userInfo)
          .then((res) => {
            if (res.code === 200) {
              window.sessionStorage.setItem('userId', res.data.userId)
              this.$router.push('/index')
              this.$message.success('登录成功')
            } else {
              this.$message.warning(res.message)
            }
          })
          .catch((err) => {
            this.$message.warning(err.message)
          })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  width: 100%;
  height: 100%;
  background-color: #ececec;
  display: flex;
  align-items: center;
  justify-content: center;
  .login-box {
    box-sizing: border-box;
    width: 700px;
    height: 500px;
    padding: 0 50px;
    background-color: #fff;
    border-radius: 10px;
    .title {
      font-size: 24px;
      text-align: center;
      font-weight: bold;
      color: #999;
      padding-bottom: 100px;
    }
    .el-form {
      .el-form-item {
        padding: 10px;
      }
    }
    .btn-box {
      box-sizing: border-box;
      padding: 50px 40px 0 100px;
      .el-button {
        width: 100%;
      }
    }
  }
}
</style>
